<?php
require('../../../wp-config.php');
$plugin_url = trailingslashit( WP_PLUGIN_URL.'/'. dirname( plugin_basename(__FILE__)));
?>
<script>
    jQuery('#upload_image_button').click(function() {
 formfield = jQuery('#upload_image').attr('name');
 tb_show('', 'media-upload.php?type=image&amp;TB_iframe=true');
 return false;
});

window.send_to_editor = function(html) {
 imgurl = jQuery('img',html).attr('src');
 lasthtml = jQuery("#display_image").html();
  jQuery("#display_image").html(lasthtml+'&nbsp;<img src="'+imgurl+'" /><img src="<?php echo $plugin_url;?>images/delete.png" onclick="remove_image(\''+imgurl+'\');" style="cursor:pointer;" /><a href="javascript:void(0);" onclick="add_main(\''+imgurl+'\');" >set main picture</a>');
  if(jQuery("#image").val() == ''){
      jQuery("#image").val(imgurl);
  }
  else
      {
          jQuery("#image").val(jQuery("#image").val()+','+imgurl);
      }
 tb_remove();
}
function remove_image(value){
    if(confirm("Are you sure you want to remove this image ? ")){
   var string = jQuery("#image").val();
   string = string.replace(value+',','');
   string = string.replace(','+value,'');
   string = string.replace(value,'');
   jQuery("#image").val(string);
   array = string.split(',');
   value = '';
   for (i in array){
       value = value +'&nbsp;<img src="'+array[i]+'" /><img src="<?php echo $plugin_url;?>images/delete.png" onclick="remove_image(\''+array[i]+'\');" style="cursor:pointer;" /><a href="javascript:void(0);" onclick="add_main(\''+array[i]+'\');" >set main picture</a>';
   }
   jQuery("#display_image").html(value);
   }
}
function add_main(value){
    if(confirm("Add this picture as main image ?")){
        jQuery("#main_image").val(value);
        string = jQuery("#image").val();
       array = string.split(',');
       value2 = '';
       for (i in array){
           if(array[i] != value){
               value2 = value2 +'&nbsp;<img src="'+array[i]+'" /><img src="<?php echo $plugin_url;?>images/delete.png" onclick="remove_image(\''+array[i]+'\');" style="cursor:pointer;" /><a href="javascript:void(0);" onclick="add_main(\''+array[i]+'\');" >set main picture</a>';
           }
           else
           {
               value2 = value2 +'&nbsp;<img src="'+array[i]+'" /><img src="<?php echo $plugin_url;?>images/delete.png" onclick="remove_image(\''+array[i]+'\');" style="cursor:pointer;" />';
           }
           }
       jQuery("#display_image").html(value2);


    }
}
</script>

<div id="loading" align="center" style="display:none;"><img src="<?php echo $plugin_url;?>images/loading.gif" /><br />Loading...</div>
<strong>Add new listing</strong>
<p></p>
<form method="post" action="<?php echo $plugin_url; ?>add_house.php" name="add_house" enctype="multipart/form-data">
<table border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td>
            Code :
        </td>
        <td>
            <input type="text" name="code" size="60" />
        </td>
        </tr>
        <tr>
        <td>
            Name :
        </td>
        <td>
            <input type="text" name="name"  size="60"  />
        </td>
    </tr>
     <tr>
        <td>
            Address :
        </td>
        <td>
            <input type="text" name="address"  size="60"  />
        </td>
    </tr>
      <tr>
        <td>
            Bedrooms :
        </td>
        <td>
            <select name="bed_room">
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4 or more</option>
            </select>
        </td>
    </tr>
    <tr>
        <td>
            Property Type :
        </td>
        <td>
            <select name="type">
	       <option value="2">Single Family</option>
               <option value="3">Cottage</option>
               <option value="4">Duplex or Condo</option>
            </select>
        </td>
    </tr>
    <tr>
        <td>
            Location :
        </td>
        <td>
            <select name="location">
		<option value="2">Barnstable</option>
                <option value="3">Boston</option>
                <option value="4">Bourne</option>
                <option value="5">Brewster</option>
                <option value="6">Cape Cod</option>
                <option value="7">Centerville</option>
                <option value="8">Chatham</option>
                <option value="9">Cohasset</option>
                <option value="10">Cotuit</option>
                <option value="11">Craigville</option>
                <option value="12">Cummaquid</option>
                <option value="13">Dennis</option>
                <option value="14">Falmouth</option>
                <option value="15">Great Island</option>
                <option value="16">Harwich</option>
                <option value="17">Hyannis</option>
                <option value="18">Hyannisport</option>
                <option value="19">Marion</option>
                <option value="20">Marstons Mills</option>
                <option value="21">Mashpee</option>
                <option value="22">Nantucket</option>
                <option value="23">New Seabury</option>
                <option value="24">Orleans</option>
                <option value="25">Osterville</option>
                <option value="26">Other</option>
                <option value="27">Pocasset</option>
                <option value="28">Popponessett</option>
                <option value="29">Sandwich</option>
                <option value="30">Willowbend</option>
                <option value="31">Woods Hole</option>
                <option value="32">Yarmouth</option>
            </select>
        </td>
    </tr>
    <tr>
        <td>
            Price :
        </td>
        <td>
            <input type="text" name="price"  size="60"  />
        </td>
    </tr>
    <tr>
        <td>
            Description :
        </td>
        <td>
            <textarea name="description" id="editorContent" cols="57" rows="15"></textarea>
        </td>
    </tr>
    <tr valign="top">
<td >Image</td>
<td><label for="upload_image">
<input id="upload_image_button" type="button" value="Add Image" />
<div id="display_image"></div>
<input type="hidden" name="image" id="image" value="" />
<input type="hidden" name="main_image" id="main_image" />
</label></td>
</tr>

</table>
<?php foreach($wpdb->get_results("SELECT * FROM wp_category") as $category):?>
<div style="background-color:#CCCCCC;color:white;font-weight: bold;margin-top:10px;"><?php echo $category->name;?></div>
<br />
<?php foreach($wpdb->get_results("SELECT * FROM wp_attribute WHERE category=".$category->id) as $row){echo '<input type="checkbox" name="attribute'.$category->id.'['.$row->id.']" value="1" />&nbsp;'.$row->name.'&nbsp;';}?>
<?php endforeach;?>
</form>
<p></p>
<a class="button add-new-h2" href="javascript:void(0);" onclick="document.add_house.submit();">Add</a>